﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLKTX1._0.DAO;
using QLKTX1._0.DTO;

namespace QLKTX1._0.GUI.CauHinh
{
    public partial class FrmDotDangKyMoi : Form
    {
        
        public FrmDotDangKyMoi()
        {
            InitializeComponent();
        }

        private void FrmDotDangKyMoi_Load(object sender, EventArgs e)
        {
            btnLuu.Enabled = false;
           txtTTDDK.ReadOnly = true;
           txtStatus.Visible = false;
            
            cmbNamHoc.DataSource = NamHocDAO.dsNamHoc();
            cmbNamHoc.DisplayMember = "tenNamHoc";
            cmbNamHoc.ValueMember = "maNamHoc";

            dtgvDotDangKy.DataSource = this.xemChiTietDotDK();
            // dat mau
            
            
            // thay doi ten cot
            dtgvDotDangKy.Columns[0].HeaderText = "Mã đợt";
            dtgvDotDangKy.Columns[1].HeaderText = "Ngày bắt đầu";
            dtgvDotDangKy.Columns[2].HeaderText = "Ngày kết thúc";
            dtgvDotDangKy.Columns[3].HeaderText = "Tên đợt";
            dtgvDotDangKy.Columns[4].HeaderText = "Tình trạng";
            dtgvDotDangKy.Columns[5].HeaderText = "Năm học";
            //Chinh do rong cua cot..............
            dtgvDotDangKy.Columns[0].Width = 85;
            dtgvDotDangKy.Columns[1].Width = 117;
            dtgvDotDangKy.Columns[2].Width = 117;
            dtgvDotDangKy.Columns[3].Width = 90;
            dtgvDotDangKy.Columns[4].Width = 100;
            dtgvDotDangKy.Columns[5].Width = 100;
            //gan du lieu gridview len textbox-------------------------------
            txtMaDot.DataBindings.Clear();
            txtMaDot.DataBindings.Add("Text", dtgvDotDangKy.DataSource, "maDot");

            dtPicker1.DataBindings.Clear();
            dtPicker1.DataBindings.Add("Text", dtgvDotDangKy.DataSource, "ngayBatDau");

            dtPicker2.DataBindings.Clear();
            dtPicker2.DataBindings.Add("Text", dtgvDotDangKy.DataSource, "ngayKetThuc");

            txtTenDot.DataBindings.Clear();
            txtTenDot.DataBindings.Add("Text", dtgvDotDangKy.DataSource, "tenDot");

            txtStatus.DataBindings.Clear();
            txtStatus.DataBindings.Add("Text", dtgvDotDangKy.DataSource, "tinhTrang");

            cmbNamHoc.DataBindings.Clear();
            cmbNamHoc.DataBindings.Add("Text", dtgvDotDangKy.DataSource, "tenNamHoc");

           
        }


        private DataTable xemChiTietDotDK()
        {
            DataTable dt = new DataTable(); // bang de chua


            DataColumn col1 = new DataColumn("maDot");
            //col1.DataType = System.Type.GetType("System.String");
            dt.Columns.Add(col1);

            DataColumn col2 = new DataColumn("ngayBatDau");
            //col2.DataType = System.Type.GetType("System.String");
            dt.Columns.Add(col2);

            DataColumn col3 = new DataColumn("ngayKetThuc");
            //col3.DataType = System.Type.GetType("System.String");
            dt.Columns.Add(col3);

            DataColumn col4 = new DataColumn("tenDot");
            //col4.DataType = System.Type.GetType("System .String");
            dt.Columns.Add(col4);


            DataColumn col5 = new DataColumn("tinhTrang");
            //col5.DataType = System.Type.GetType("System .String");
            dt.Columns.Add(col5);

            DataColumn col6 = new DataColumn("tenNamHoc");
            //col6.DataType = System.Type.GetType("System .Int32");
            dt.Columns.Add(col6);

            foreach (DotDangKyDTO  dot in DotDangKyMoiDAO .dsDotDangKy())
            {
                dt.AcceptChanges();

                DataRow dr = dt.NewRow(); // tao 1 hang
                               
                dr[0] = dot.MaDot;
                dr[1] = dot.NgayBatDau.ToString("yyyy-MM-dd");
                dr[2] = dot.NgayKetThuc.ToString("yyyy-MM-dd");
                dr[3] = dot.TenDot;
                dr[4] = dot.TinhTrang;
                dr[5] = dot.MaNamHoc.TenNamHoc  ;

               

                dt.Rows.Add(dr);

            }

            return dt;
        }

        private void btnlThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text.Equals("Thêm"))
            {
                btnCapNhat.Enabled = false;
                btnXoa.Enabled = false;
                btnLuu.Enabled = true;
                txtMaDot .Text = Service.generateNoAuto("Dot_DangKy", "maDot", "D", 1);//Tao ma tu dong
                //txtMLCP.Text = "";
                txtTenDot.Text = "";
               
                btnThem.Text = "Hủy bỏ";

            }
            else
            {
                btnThem.Text = "Thêm";
                btnCapNhat.Enabled = true;
                btnXoa.Enabled = true;
                btnLuu.Enabled = false;
                this.FrmDotDangKyMoi_Load(sender, e);
            }
        }

        private void cb1_TextChanged(object sender, EventArgs e)
        {
            if (txtStatus.Text.Equals("1"))
            {
                txtTTDDK.Text = "ĐẢ KẾT THÚC";
                tbnAU.Enabled = false;
            }
            else if (txtStatus.Text.Equals("2"))
            {
                txtTTDDK.Text = "ĐANG MỞ";
                tbnAU.Enabled = true;
                tbnAU.Text = "ĐÓNG ĐỢT";
            }
            else
            {
                txtTTDDK.Text = "ĐANG LẬP KẾ HOẠCH";
                tbnAU.Enabled = true;
                tbnAU.Text = "MỞ ĐỢT";
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (btnCapNhat.Text.Equals("Cập Nhật"))
            {
                btnLuu.Enabled = true;
                btnThem.Enabled = false;
                btnXoa.Enabled = false;
                btnCapNhat.Text = "Hủy bỏ";
                // this.FrmLoaiChiPhi_Load(sender, e);

            }
            else
            {
                btnLuu.Enabled = false;
                btnThem.Enabled = true;
                btnXoa.Enabled = true;

                btnCapNhat.Text = "Cập Nhật";
                this.FrmDotDangKyMoi_Load(sender, e);
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (btnThem.Text.Equals("Hủy bỏ"))
            {
                if (txtTenDot.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng nhập dữ liệu", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {

                    DotDangKyDTO dot = new DotDangKyDTO();
                    dot.MaDot = txtMaDot.Text;
                    dot.NgayBatDau = DateTime.ParseExact(dtPicker1.Text, "dd/MM/yyyy", null);
                    dot.NgayKetThuc= DateTime.ParseExact(dtPicker2.Text, "dd/MM/yyyy", null);
                    dot.TenDot = txtTenDot.Text;              

                    int namhoc = Int32.Parse (cmbNamHoc.SelectedValue.ToString());
                    NamHocDTO nh = new NamHocDTO();
                    nh.MaNamHoc = namhoc;
                    dot.MaNamHoc = nh;
                    dot.TinhTrang = 3; // dang lap ke hoach

                    DotDangKyMoiDAO.themDotDangKy(dot);
                    btnThem.Text = "Thêm";
                    btnCapNhat.Enabled = true;
                    btnXoa.Enabled = true;
                    btnThem.Enabled = true;
                    this.FrmDotDangKyMoi_Load(sender, e);
                }
            }
            else
            {
                if (txtTenDot.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng nhập dữ liệu", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    DotDangKyDTO dot = DotDangKyMoiDAO.getObject(txtMaDot.Text);
                    dot.NgayBatDau = DateTime.ParseExact(dtPicker1.Text, "dd/MM/yyyy", null);
                    dot.NgayKetThuc = DateTime.ParseExact(dtPicker2.Text, "dd/MM/yyyy", null);
                    dot.TenDot = txtTenDot.Text;            
                    int namhoc = Int32.Parse(cmbNamHoc.SelectedValue.ToString());
                    NamHocDTO nh = new NamHocDTO();
                    nh.MaNamHoc = namhoc;
                    dot.MaNamHoc = nh;
                    DotDangKyMoiDAO.capNhatDotDangKy(dot);               

                    btnCapNhat.Text = "Cập Nhật";
                    btnCapNhat.Enabled = true;
                    btnXoa.Enabled = true;
                    btnThem.Enabled = true;
                    this.FrmDotDangKyMoi_Load(sender, e);
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa ?", "Xác nhận xóa ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                DotDangKyDTO dot = new DotDangKyDTO();
               

                dot.MaDot = txtMaDot.Text;

                DotDangKyMoiDAO.xoaDotDangKy(dot);

                this.FrmDotDangKyMoi_Load(sender, e);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtStatus.Text.Equals("1"))
            {
                MessageBox.Show("Dot nay da ket thuc, ban khong the mo/dong dc ?", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (txtStatus.Text.Equals("2"))
            {
                if (MessageBox.Show("Dot nay dang mo, ban co chac la dong dot khong", "Xác nhận xóa ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DotDangKyDTO d = DotDangKyMoiDAO.getObject(txtMaDot.Text);

                    d.TinhTrang = 1;

                    DotDangKyMoiDAO.capNhatDotDangKy(d);
                }
            }
            else
            { // kiem tra xem dot nao con mo khong
                if (!ValidateData.checkDataExistabled("Dot_DangKy", "tinhTrang", "2"))
                {
                    if (MessageBox.Show("Dot nay dang lap ke hoach,ban co chac la muon mo khong ", "Xác nhận xóa ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        DotDangKyDTO d = DotDangKyMoiDAO.getObject(txtMaDot.Text);

                        d.TinhTrang = 2;

                        DotDangKyMoiDAO.capNhatDotDangKy(d);
                    }
                }
                else
                {
                    MessageBox.Show("Dang co 1 dot dang mo, ban khong the mo tiep", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

            this.FrmDotDangKyMoi_Load(sender, e);
        }

        private void dtgvDotDangKy_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
           
        }

        private void dtgvDotDangKy_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            foreach (DataGridViewRow row in dtgvDotDangKy.Rows)
            {
                if (Convert.ToInt32(row.Cells[4].Value) == 1)
                {
                    row.DefaultCellStyle.BackColor = Color.Red;
                    row.DefaultCellStyle.ForeColor = Color.White;
                }
                else if (Convert.ToInt32(row.Cells[4].Value) == 2)
                {
                    row.DefaultCellStyle.BackColor = Color.GreenYellow;
                }
                else
                {
                    row.DefaultCellStyle.BackColor = Color.Yellow;
                }               
            } 
        }
    }
}
